import 'dart:convert';
import 'dart:developer' as Develop;
import 'package:flutter/material.dart';
import 'dart:math';

extension StringExtension on String {
  Map<String, dynamic> toMap() {
    return json.decode(this);
  }

  toImage({width}) {
    if (startsWith("http")) {
      return Image.network(this);
    }
    return Image.asset(
      "asset/images/$this.png",
      width: width,
    );
  }

  Color toColor() {
    final buffer = StringBuffer();
    if (length == 6 || length == 7) buffer.write('ff');
    buffer.write(replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  }

  withHost() {
    if (startsWith("http")) {
      return this;
    }
    return "http://wangshuguan.pythonanywhere.com${this}";
  }

  encryptString() {
    return replaceRange(max(0, (length - 1 - 4)), length - 1, "****");
  }
}

extension MapExtension on Map {
  String toJSONString() {
    return json.encode(this);
  }
}

extension ListExtension on List {
  String toJSONString() {
    return json.encode(this);
  }
}

typedef void EventCallback(arg);

void UtilLog(Object message) {
  Develop.log("${DateTime.now()}:$message");
}
